<?php
//取用户信息
function user_get($id= 0, $is_no_cache = 0) {
	return is_array($id) ? $id : cache_get_table_obj('user', $id, $is_no_cache);
}
function user_mobile_get($mobile){
	return db_get_obj('user', array('mobile' => $mobile));
}
function user_get_by_openid($openid = '', $is_no_cache = 0) {
    $ret = FALSE;
    if($openid != '') {
        $key = 'user_openid_'.$openid;
        $obj = cache_get($key);
        if(!$obj || $is_no_cache == 1) {
            $obj = db_get_obj('user', ['openid' => $openid]);
            if($obj) {
                cache_save($key, $obj);
            }
        }
        if($obj) {
            $ret = $obj;
        }
    }
    return $ret;
}

function user_update($user, $up_data) {
    $ret = FALSE;
    if($user) {
        db_update('user', $up_data, ['id' => $user['id']]);
        $ret = user_get($user['id'], 1);
    }
    return $ret;
}
function check_user_openid($url){
	@session_start();
	if($_SESSION['openid']){

	}else{
		$t = get_parameter('t', '');
		if($t == 123){
			$app = weixin_get_app();
			$weixin_user = $app->oauth->user();
			$_SESSION['openid'] = $weixin_user->getId();
			$open_user = db_get_obj('user', array('openid' => $weixin_user->getId()));
			if( ! $open_user){
				insert_openid_user($weixin_user);
			}
		}else{
			user_openid_get($url.'?t=123');
		}
	}
}
function user_openid_get($url){
	$app = weixin_get_app();
	$response = $app->oauth->scopes(['snsapi_base'])->redirect($url);
	dump($response);
//	$response->send(); exit();
}
function insert_openid_user($weixin_user){
	$up_data = [
		'nickname' => $weixin_user['user_info']['nickname'],
		'sex' => $weixin_user['user_info']['sex'],
		'language' => $weixin_user['user_info']['language'],
		'city' => $weixin_user['user_info']['city'],
		'province' => $weixin_user['user_info']['province'],
		'country' => $weixin_user['user_info']['country'],
		'headimgurl' => $weixin_user['user_info']['headimgurl'],
		'subscribe_time' => $weixin_user['user_info']['subscribe_time'],
		'remark' => $weixin_user['user_info']['remark'],
		'groupid' => $weixin_user['user_info']['groupid'],
		'unionid' => $weixin_user['user_info']['unionid'] ? $weixin_user['user_info']['unionid'] : 0,
		'openid' => $weixin_user['user_info']['openid'],
		'is_unsubscribe' => 0,
		'is_subscribed' => 1
	];
//	$user = user_get_by_openid($weixin_user['user_info']['openid'], 1);
	$user = db_get_obj('user', array('openid' => $weixin_user['user_info']['openid']));
	if($user) {
		user_update($user, $up_data);
	} else {
		$up_data['ctime'] = time();
		db_insert('user', $up_data);
	}
}
define('LOGIN_SEESION_NAME', 'weixin_user');
//取登录用户信息
function user_login_get() {
	$ret = false;
	@session_start();
	$login_id = isset($_SESSION[LOGIN_SEESION_NAME]) ? $_SESSION[LOGIN_SEESION_NAME] : 0;
	if( $login_id > 0 ) {
		$ret = user_get($login_id);
		if( @$ret['state'] < 0 ) {
			// 用户被禁用
			$ret = false;
		}
	}

	return $ret;
}
//用户退出
function user_logout() {
	@session_start();
	unset($_SESSION[LOGIN_SEESION_NAME]);
}
//用户登录
function user_login( $user ) {
	@session_start();
	$_SESSION[LOGIN_SEESION_NAME] = $user['id'];
	$user = user_get($user['id'], 1);
}
function sex($sex){
	if($sex == 1){
		return '男';
	}elseif($sex == 2){
		return '女';
	}else{
		return '保密';
	}
}
function age($birthday){
	$ret = '';
	if($birthday){
		$birthday = date_str($birthday);
		$ret = date('Y', time()) - date('Y', strtotime($birthday)) - 1;
		if (date('m', time()) == date('m', strtotime($birthday))){

			if (date('d', time()) > date('d', strtotime($birthday))){
				$ret++;
			}
		}elseif (date('m', time()) > date('m', strtotime($birthday))){
			$ret++;
		}
//		$ret = (time() - $birthday) / 31536000;
	}
	return $ret;
}
//member, staff
function b_qrcode_member($qrcode_id, $from_username){
    file_put_contents(FCPATH.'log/scan', $qrcode_id.'-'.$from_username);
//    $str = str_replace('qrscene_', '', $qrcode_id);
    $id = (int)str_replace('qrscene_', '', $qrcode_id);
    if($id > 0){
        $qrcode_data = db_get_obj('qrcode', ['id' => $id]);
        if ($qrcode_data['scan_uid'] == 0) {
            $q_urse = user_get($qrcode_data['uid'], 1);
            if ($q_urse['is_rec'] == 1) {
                user_update($q_urse, ['reward' => $q_urse['reward'] + 20]);
            }
            $user = user_get_by_openid($from_username, 1);
            db_update('qrcode', ['scan_uid' => $user['id']], ['id' => $id]);
        }
    }
}
function user_get_by_mid($mid){
    $member = member_get($mid);
    $user = user_get($member['muid']);
    return $user;
}
//获取关注信息
function get_follow($uid, $ta_uid){
    $r = db_get_obj('follow', array('uid' => $uid, 'ta_uid' => $ta_uid));
    return $r;
}
function get_favorite($uid, $trend_id){
    $r = db_get_obj('favorite', array('uid' => $uid, 'article_id' => $trend_id));
    return $r;
}
function get_user_shop($uid){
    $ret = [];
    $l = db_get_list('member', ['muid' => $uid, 'state' => 1], '', '', '', 'sid');
    if ($l) {
        $w = [];
        foreach ($l as $k => $v) {
            $w[$k] = $v['sid'];
        }
        $w = implode(',', $w);
        $ret = db_get_list('Goods', 'id in(' . $w . ')');
    }
    return $ret;
}
//判断学生学段
function period($grade){
    $ret = '';
    if ($grade <= 5) {
        $ret = '高中';
    } elseif ($grade >= 6 && $grade <= 11) {
        $ret = '初中';
    } elseif ($grade >= 12) {
        $ret = '小学';
    }
    return $ret;
}
function period_id($grade){
    $ret = 0;
    if ($grade <= 5) {
        $ret = 3;
    } elseif ($grade >= 6 && $grade <= 11) {
        $ret = 2;
    } elseif ($grade >= 12) {
        $ret = 1;
    }
    return $ret;
}